package org.thingsboard.server.schedule.listener;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.listeners.SchedulerListenerSupport;
import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.SchedulerEventInfo;
import org.thingsboard.server.common.data.SchedulerEventStatus;
import org.thingsboard.server.dao.schedulerEvent.SchedulerEventService;
import org.thingsboard.server.dao.schedulerEvent.SchedulerEventServiceImpl;
import org.thingsboard.server.utils.SpringUtil;

@Slf4j
@Component
public class CustomSchedulerListener extends SchedulerListenerSupport {
    public static final String JOB_KEY = "TASK_KEY";
    /**
     *  触发器不会再触发 过期 时
     *  手动触发 会触发此功能
     * @param trigger
     */
    @Override
    public void triggerFinalized(Trigger trigger) {
        if(trigger.getJobDataMap().get("manual")==null){
            SchedulerEventService schedulerEventService=SpringUtil.getBean(SchedulerEventServiceImpl.class);
            SchedulerEventInfo schedulerEventInfo= (SchedulerEventInfo) trigger.getJobDataMap().get(JOB_KEY);
            schedulerEventInfo.setStatus(SchedulerEventStatus.EXPIRE);
            schedulerEventService.saveSchedulerEventInfo(schedulerEventInfo);
        }
    }


}
